<?php
include_once('/var/www/html/new/com.inc.php');
include_once('/var/www/html/new/d/class/Terminal_Check_Socket.class.php');

// 接收数据
$token   = $_REQUEST['token'];
$orderid = $_REQUEST['id'] + 0;
$month   = date('Ymd');
write_logs("[$orderid]订单验证---".json_encode($_REQUEST), "/var/www/html/new/d/logs/terminal_check/terminal_check_jt_d_$month.txt");

if($orderid==0) exit('201');
if(md5($orderid - 999) != $token) Response("[$orderid]认证失败:$token", 202);

$ac = $GLOBALS['_ac'];
$pw = $GLOBALS['_pw'];
$s  = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));

$resxml = $s->Order_Globle_Search("","","","","","","","","","","","", $orderid,"","","","","",1,1,0,1,"","","","","","","","","",0,1);
$simxml = simplexml_load_string($resxml);
if(!isset($simxml->Rec)) Response("[$orderid]订单查找失败", 204);

$salerid = (string)$simxml->Rec->UUsalerid;
$voucher = (string)$simxml->Rec->UUcode;


if((int)$simxml->Rec->UUstatus==3) Response("[$orderid]订单已取消", 105);
// if((int)$simxml->Rec->UUstatus==1 && (string)$simxml->Rec->UUpack_order=='0') Response("[$orderid]订单已验证", 1077);
if((int)$simxml->Rec->UUstatus==1) Response("[$orderid]订单已验证", 1077);

if(strlen($salerid)==8){// 终端合并新终端号
	$sql = "select nowTerminal,nowSalerID from pft_terminal_share where nowSalerID='$salerid' limit 1";
	$GLOBALS['le']->query($sql);
	if(!$GLOBALS['le']->fetch_assoc()) Response("[$orderid]未找到景区", 203);
	$salerid       = $GLOBALS['le']->f('nowSalerID');
	$terminal_inum = $GLOBALS['le']->f('nowTerminal');
	$terminal_type = 1;
}else{
	$sql = "select terminal,terminal_type,salerid from uu_land where salerid=$salerid limit 1";
	$GLOBALS['le']->query($sql);
	if(!$GLOBALS['le']->fetch_assoc()) Response("[$orderid]未找到景区", 203);
	$terminal_inum = $GLOBALS['le']->f('terminal');
	$terminal_type = $GLOBALS['le']->f('terminal_type');
}


$tc = Terminal_Check_Socket::connect();
$chkIns = 499;

if((string)$simxml->Rec->UUstatus==2)  $chkIns = 498;
if((string)$simxml->Rec->UUpack_order!='0') $chkIns = 260;// 套票子票用260

$chResult = $tc->Terminal_Check_In_Voucher($terminal_inum, $salerid, $voucher, array('vCmd'=>$chkIns, 'vMode'=>7));
if($chResult['state']=='success' && isset($chResult['multi'])){// 同一景区子票多单
	$tc = new Terminal_Check_Socket();
	$chResult = $tc->Terminal_Check_In_Voucher($terminal_inum, $salerid, $chResult['tickets'][0]['ticket_code'], array('vCmd'=>$chkIns, 'vMode'=>7));
}

if($chResult['state']=='success' && $chResult['ordernum']!='') {
	
	Response("[$orderid]验证成功", 200);
}else{
	
	$chResult_s = json_encode($chResult);
	Response("[$orderid]验证失败-$chResult_s", 255);
}



function Response($text, $status){
	
	$month = date('Ymd');
	if($status==200){
		write_logs($text, "/var/www/html/new/d/logs/terminal_check/terminal_check_jt_s_$month.txt");
		exit('200');
	}
	
	write_logs($text, "/var/www/html/new/d/logs/terminal_check/terminal_check_jt_e_$month.txt");
	exit("$status");
	// exit('200');
}

function write_logs($txt,$file){
	$fp = fopen($file,"a");
	flock($fp, LOCK_EX);
	fwrite($fp,date("Y-m-d H:i:s").":".$txt."\n");
	flock($fp, LOCK_UN);
	fclose($fp);
}